/*******************************************************************************
 * Chatty - Mobile Messaging - J2ME ICQ clone
 * Copyright (C) 2003-05  Chatty Project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
 * File: src/chatty/ChattyException.java
 * Version: ###VERSION###  Date: ###DATE###
 * Author(s): Manuel Linsmayer, Andreas Rossbacher, Vladimir Kryukov
 *******************************************************************************/


package chatty;

import javax.microedition.lcdui.*;

import chatty.comm.*;
import chatty.util.*;
import chatty.modules.*;
import chatty.cl.ContactList;


public class ChattyException extends Exception {
    private int errorCode;
    private int errorCodeExt;
    private boolean critical;
	
    // Constructs a critical ChattyException
	public ChattyException(int errCode, int extErrCode) {
        super(ResBundle.getString("error_" + errCode)
                + " (" + errCode + "." + extErrCode + ")");
        this.errorCode = errCode;
        this.errorCodeExt = extErrCode;
        this.critical = true;
    }
    
    // Constructs a non-critical ChattyException
    public ChattyException(int errCode, int extErrCode, boolean displayMsg) {
        this(errCode, extErrCode);
        this.critical = false;
    }
    
    // Returns true if this is a critical exception
    public boolean isCritical() {
        return this.critical;
    }
    
    public boolean isReconnectable() {
        return Options.getBoolean(Options.OPTION_RECONNECT) && isCritical()
                && (errorCode < 110 || errorCode > 120 || errorCode == 118)
                && errorCode != 123 && errorCode != 124 && errorCode != 127 && errorCode != 140;
    }

    public String getTitle() {
        return ResBundle.getString(isCritical() ? "error" : "warning")
                    + " #" + errorCode + "." + errorCodeExt;
    }
    // Exception handler
    public static void handleException(ChattyException e) {
        handleException(e, null);
    }
    public static void handleException(ChattyException e, String userId) {
        // #sijapp cond.if modules_LIGHT is "true" #
        CustomLight.setLightMode(CustomLight.ACTION_ERROR);
        // #sijapp cond.end#
        Chatty.unlockChatty();
        ContactList.activateContactList();
        e.showAlert(userId);
    }
    public void showAlert(String userId) {
        String message = getMessage();
        if (null != userId) {
            message = userId + "\n" + message;
        }
        new chatty.ui.PopupWindow(getTitle(), message).show();
    }
}
